/*
 * Copyright (c) 2017. 杭州端点网络科技有限公司.  All rights reserved.
 */

package com.jiantong.user.impl.service;

import com.google.common.base.Strings;
import com.google.common.base.Throwables;
import com.jiantong.user.enums.DepartmentType;
import com.jiantong.user.impl.dao.DeclareStationDao;
import com.jiantong.user.impl.dao.DepartmentDao;
import com.jiantong.user.impl.manager.UserManager;
import com.jiantong.user.model.DepartmentUser;
import com.jiantong.user.model.User;
import com.jiantong.user.service.DepartmentUserWriteService;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author : panxin
 */
@Slf4j
@Service
@RpcProvider
public class DepartmentUserWriteServiceImpl implements DepartmentUserWriteService {

    private final DepartmentDao departmentDao;
    private final DeclareStationDao declareStationDao;
    private final UserManager userManager;

    @Autowired
    public DepartmentUserWriteServiceImpl(DepartmentDao departmentDao, DeclareStationDao declareStationDao, UserManager userManager) {
        this.departmentDao = departmentDao;
        this.declareStationDao = declareStationDao;
        this.userManager = userManager;
    }

    @Override
    public Response<Boolean> createNormalDepartmentUser(User user, DepartmentUser departmentUser) {
        try {
            // TODO check 用户角色、关联的部门信息是否正确
            // departmentDao.xxxXXX

            if (Strings.isNullOrEmpty(user.getName())) {
                log.error("user name is empty.");
                return Response.fail("user.name.is.null");
            }

            // 这拉创建的都应该属于政府机构下的普通部门
            departmentUser.setUserName(user.getName());
            departmentUser.setDepartmentType(DepartmentType.NORMAL_DEPARTMENT.value());

            return Response.ok(userManager.createDepartmentUser(user, departmentUser));
        }catch (Exception e) {
            log.error("failed to create department user, user = {}, department user = {}, cause : {}",
                    user, departmentUser, Throwables.getStackTraceAsString(e));
            return Response.fail("create.department.user.failed");
        }
    }

    @Override
    public Response<Boolean> createDeclareStationUser(User user, DepartmentUser departmentUser) {
        try {
            // TODO check 用户角色、关联的部门信息是否正确
            // declareStationDao.xxxXXX

            if (Strings.isNullOrEmpty(user.getName())) {
                log.error("user name is empty.");
                return Response.fail("user.name.is.null");
            }

            // 这里创建你的部门类型要么是申报点、要么是管理屠宰场的
            departmentUser.setUserName(user.getName());

            return Response.ok(userManager.createDepartmentUser(user, departmentUser));
        }catch (Exception e) {
            log.error("failed to create declare user, user = {}, department user = {}, cause : {}",
                    user, departmentUser, Throwables.getStackTraceAsString(e));
            return Response.fail("create.department.user.failed");
        }
    }
}
